package com.gis3c.online.service.impl;

import com.gis3c.common.exception.BusinessException;
import com.gis3c.online.dao.PollutionSourceDao;
import com.gis3c.online.entry.PollutionSourceStation;
import com.gis3c.online.entry.StationStatus;
import com.gis3c.online.service.PollutionSourceService;
import com.gis3c.spatial.common.FeatureUtilities;
import com.gis3c.spatial.common.GeometryUtilities;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by hukekuan on 2018/3/23.
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class PollutionSourceServiceImpl implements PollutionSourceService {
    @Autowired
    private PollutionSourceDao pollutionSourceDao;

    @Override
    public String findStationByType(Integer subType, Integer pollCode,Map<String,Object> queryParams) {
        String result = null;
        List<PollutionSourceStation> stationList
                = pollutionSourceDao.findStationByType(subType, pollCode,queryParams);
        List<PollutionSourceStation> stationList_status = null;
        List<StationStatus> stationStatusList
                = pollutionSourceDao.findStationStatusByType(subType, pollCode);
        Set<StationStatus> queryStatus;

        for (PollutionSourceStation pollutionSourceStation : stationList) {
            queryStatus = stationStatusList.stream()
                    .filter(stationStatus -> pollutionSourceStation.getStationId().equals(stationStatus.getStationId())
                            && stationStatus.GetStatusCode() < 0).collect(Collectors.toSet());
            if (queryStatus != null && queryStatus.size() > 0) {
                for (StationStatus stationStatus : queryStatus) {
                    if (stationStatus.GetStatusCode() == -1.0) {
                        pollutionSourceStation.setStatusCode(-1.0);
                        break;
                    } else {
                        pollutionSourceStation.setStatusCode(stationStatus.GetStatusCode());
                    }
                }
            } else {
                pollutionSourceStation.setStatusCode(0.0);
            }
            pollutionSourceStation.setPollItems(null);
            pollutionSourceStation.setGeometry(
                    GeometryUtilities.CreatePoint(
                            pollutionSourceStation.getLongitude(),
                            pollutionSourceStation.getLatitude())
            );
        }
        stationList = stationList
                .stream()
                .filter(pollutionSourceStation -> pollutionSourceStation.getAdminCode() != null)
                .collect(Collectors.toList());
        if(!queryParams.keySet().contains("statusCodes")
                || ((Set<Integer>)queryParams.get("statusCodes")).size() == 0){
            stationList_status = stationList;
        }else {
            Set<Integer> statusCodes = (Set<Integer>)queryParams.get("statusCodes");
            stationList_status = stationList.stream().filter(pollutionSourceStation ->{
                return ((pollutionSourceStation.getStatusCode() > 0 && statusCodes.contains(1)) ||
                        (pollutionSourceStation.getStatusCode() <= 0 && statusCodes.contains(pollutionSourceStation.getStatusCode().intValue())));
            }).collect(Collectors.toList());
        }

        try{
            result = FeatureUtilities.JavaBeans2Json(stationList_status);
        }catch (IllegalAccessException | IOException e) {
            throw new BusinessException("AirStation实体转GeoJSON出错");
        }
        return result;
    }
}
